package com.wind.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.wind.dto.req.ArticleReqDto;
import com.wind.dto.resp.ArticleRespDto;
import com.wind.entity.Article;
import com.wind.service.IArticleService;
import com.wind.util.BeanCopyUtils;
import com.wind.vo.ResultData;
import com.wind.vo.ResultDataGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName ArticleController
 * @Description Hey, it's compiled! Pack it!
 * @Author Administrator
 * @Date 2021/5/20 14:13
 * @Version 1.0
 */
@Api(tags = "文章相关接口")
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private IArticleService articleService;

    @ApiOperation("所有文章")
    @GetMapping("/list")
    public ResultData<List<Article>> list(){
        Wrapper wrapper = new EntityWrapper<Article>();
        wrapper.eq("is_active", 1);
        wrapper.orderBy("create_time", false);
        List<Article> list = articleService.selectList(wrapper);
        return ResultDataGenerator.genSuccessResult(list, "查询成功");
    }
    @ApiOperation("详细信息")
    @GetMapping("/view/{id}")
    public ResultData<Article> listByid(@PathVariable Integer id){
        Wrapper wrapper = new EntityWrapper<Article>();
        wrapper.eq("id",id);
        wrapper.eq("is_active",1);
        Article article = articleService.selectOne(wrapper);
        if(Objects.isNull(article)){
            return ResultDataGenerator.genFailResult(null,"文章不存在");
        }
        return ResultDataGenerator.genSuccessResult(article,"查询成功");
    }
}
